//
//  XNExpenseViewController.m
//  XINNingOA
//
//  Created by samuel on 2021/7/13.
//

#import "XNExpenseViewController.h"
#import "XNExpenseApproveRequest.h"
#import "XNExpenseBillInfoRequest.h"
#import "XNExpenseBillInfoModel.h"

@interface XNExpenseViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) XNExpenseBillInfoModel *infoModel;

@end

@implementation XNExpenseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [SVProgressHUD show];
    __weak typeof(self)weakSelf = self;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    XNExpenseBillInfoRequest *infoRequest = [[XNExpenseBillInfoRequest alloc] initWithOrderId:self.orderId];
    [infoRequest startWithCompletionBlockWithSuccess:^(__kindof XNExpenseBillInfoRequest * _Nonnull request) {
        weakSelf.infoModel = request.infoModel;
        dispatch_group_leave(group);
    } failure:^(__kindof XNExpenseBillInfoRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_enter(group);
    XNExpenseApproveRequest *approveRequest = [[XNExpenseApproveRequest alloc] initWithOrderId:self.orderId];
    [approveRequest startWithCompletionBlockWithSuccess:^(__kindof XNExpenseApproveRequest * _Nonnull request) {
        weakSelf.approveArray = request.approveFlowArray;
        dispatch_group_leave(group);
    } failure:^(__kindof XNExpenseApproveRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_notify(group, queue, ^{
        [weakSelf creatTableData];

        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            [weakSelf.tableView reloadData];
        });

    });
    
}


- (void)creatTableData
{
    XNTableShowDataModel *infoModel = [[XNTableShowDataModel alloc] init];
    infoModel.showType = XNShowType_Normal;
    infoModel.title = @"单据信息";
    XNShowItemModel *itemModel = [[XNShowItemModel alloc] init];
    XNUserModel *creatUser = self.infoModel.createUserObject;
    if (self.infoModel.itemList.count > 0) {
        XNExpenseItemModel *mainPage = self.infoModel.itemList[0];
        itemModel.showOverImage = mainPage.budgetItemObject.remainApplyMoney < 0;
    }
    NSArray *showItemArray = [NSArray arrayWithObjects:
                              [NSString stringWithFormat:@"单据编号： %@",self.infoModel.ID],
                              [NSString stringWithFormat:@"制单日期： %@",self.infoModel.createTimeStr],
                              [NSString stringWithFormat:@"制单人： %@",self.infoModel.createUserName],
                              [NSString stringWithFormat:@"制单部门： %@",self.infoModel.departmentName],
                              [NSString stringWithFormat:@"职务： %@",creatUser.positionName],
                              nil];
    itemModel.showItemArray = showItemArray;
    [infoModel.showDataArray addObject:itemModel];
    [self.showArray addObject:infoModel];
    
    XNTableShowDataModel *submitApplyModel = [[XNTableShowDataModel alloc] init];
    submitApplyModel.showType = XNShowType_Normal;
    submitApplyModel.title = @"申请信息";
    XNShowItemModel *itemModel2 = [[XNShowItemModel alloc] init];
    NSArray *showItemArray2 = [NSArray arrayWithObjects:
                               [NSString stringWithFormat:@"申请人: %@",self.infoModel.createUserName],
                               [NSString stringWithFormat:@"申请部门： %@",self.infoModel.departmentName],
                               [NSString stringWithFormat:@"申请类别： %@(%@)",self.infoModel.duringType,self.infoModel.duringTypeValue],
                               [NSString stringWithFormat:@"公司费用申请： %@",self.infoModel.isCompanyCost ? @"是": @"否"],
                               [NSString stringWithFormat:@"是否暂支： %@",self.infoModel.isTemp ? @"是": @"否"],
                               [NSString stringWithFormat:@"是否紧急： %@",self.infoModel.isUrgent ? @"是": @"否"],
                               [NSString stringWithFormat:@"申请事由： %@",self.infoModel.remark],
                               @"附件信息：",
                               nil];
    itemModel2.showItemArray = showItemArray2;
    itemModel2.fileArray = self.infoModel.fileList;
    itemModel2.fileIsImage = NO;
    [submitApplyModel.showDataArray addObject:itemModel2];
    [self.showArray addObject:submitApplyModel];
    
    if (self.infoModel.isTemp) {
        XNTableShowDataModel *reimbursementModel = [[XNTableShowDataModel alloc] init];
        reimbursementModel.showType = XNShowType_Normal;
        reimbursementModel.title = @"暂支单";
        XNShowItemModel *itemModel3 = [[XNShowItemModel alloc] init];
        NSArray *showItemArray3 = [NSArray arrayWithObjects:
                                   [NSString stringWithFormat:@"暂支事由： %@",self.infoModel.tempReason],
                                   [NSString stringWithFormat:@"暂支金额： %.3f",self.infoModel.tempCost],
                                   [NSString stringWithFormat:@"预计归还日期： %@",self.infoModel.returnDateStr],
                                   nil];
        itemModel3.showItemArray = showItemArray3;
        [reimbursementModel.showDataArray addObject:itemModel3];
        [self.showArray addObject:reimbursementModel];
    }
    
    
    XNTableShowDataModel *showModel4 = [[XNTableShowDataModel alloc] init];
    showModel4.showType = XNShowType_Normal;
    showModel4.title = @"子表信息";
    showModel4.headTitleArray = [NSArray arrayWithObjects:
                                         [NSString stringWithFormat:@"总计：%.3f",self.infoModel.totalCost],
                                         nil];
    [self.infoModel.itemList enumerateObjectsUsingBlock:^(XNExpenseItemModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *itemModel4 = [[XNShowItemModel alloc] init];
        NSArray *showItemArray4 = [NSArray arrayWithObjects:
                                       [NSString stringWithFormat:@"预算项目: %@",obj.budgetTypeObject.budgetTypeName],
                                       [NSString stringWithFormat:@"原始预算金额： %.3f",obj.budgetItemObject.allMoney],
                                       [NSString stringWithFormat:@"单位： %@",obj.unit],
                                       [NSString stringWithFormat:@"数量： %ld",obj.num],
                                   [NSString stringWithFormat:@"单价: %.3f",obj.price],
                                   [NSString stringWithFormat:@"本次申请金额： %.3f",obj.currentBudget],
                                   [NSString stringWithFormat:@"分批报销： %@",obj.isBatches ? @"是":@"否"],
                                   [NSString stringWithFormat:@"备注： %@",obj.remark],
                                   [NSString stringWithFormat:@"预算说明: %@",obj.budgetItemObject.showStr],
                                       nil];
        itemModel4.showItemArray = showItemArray4;
        [showModel4.showDataArray addObject:itemModel4];
    }];
    [self.showArray addObject:showModel4];
    
    
    XNTableShowDataModel *approveModel = [[XNTableShowDataModel alloc] init];
    approveModel.showType = XNShowType_Approve;
    approveModel.title = @"审批流程";
    [self.approveArray enumerateObjectsUsingBlock:^(XNBillApproveModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *temp = [[XNShowItemModel alloc] init];
        temp.showItemArray = [NSArray arrayWithObject:obj];
        [approveModel.showDataArray addObject:temp];
    }];
    [self.showArray addObject:approveModel];
}

@end
